Filter is Java Class that is used to intercept HTTP Requests and Responses.
Filters perform additional operations before sending
● HTTP Request to the Controller (it can modify HTTP Request before it gets to Controller)
● HTTP Response from the Controller (it can modify HTTP Response before it is returned to the User)
Every Filter is called twice (because HTTP Requests and Responses pass through the same Filters)
● first during HTTP Request
● then during HTTP Response
Your custom Filter Class needs
● to implement Filter Interface (so that Spring would know how to use it)
● to @Override doFilter() Method (contains actual useful code performed by the Filter)
● @Component Annotation (for Spring to detect it, create Filter Object, ads it to Filter Chain)
Filters are used for (same as Interceptors)
● Security (create Authentication Object from JWT Authorities)
● Logging (log HTTP Requests/Responses: User, Endpoint, HTTP Response)
● Error Handling (give feedback to User if HTTP Request has invalid Parameters/Format)
Filter is called twice
Filter Chain is ordered set of Filters that are
● called in sequence before sending HTTP Request to the Controller
● called in reverse order before sending HTTP Response from the Controller
Filter Chain allows you to organize complex Filter Logic into multiple Filter Classes (for easier maintenance).
Filters are called in reverse order during HTTP Response